//
//  CZDetailCommentCell.swift
//  GZWeibo05
//
//  Created by zhangping on 15/11/19.
//  Copyright © 2015年 zhangping. All rights reserved.
//

import UIKit

class DetailCommentCell: UITableViewCell {
    
    
    // MARK: - 微博模型
    var model: YSZComment? {
        didSet {
            // 设置视图内容
            // 用户头像
            if let iconUrl = model?.user?.profile_image_url {
                iconView.sd_setImageWithURL(NSURL(string: iconUrl))
            }
            
            // 名称
            nameLabel.text = model?.user?.screen_name
            
            // 时间
            timeLabel.text = model?.created_at ?? "未知时间"
            
            // 认证类型
            // 判断类型设置不同的图片
            // 没有认证:-1   认证用户:0  企业认证:2,3,5  达人:220
            verifiedView.image = model?.user?.verifiedImage
            // 会员等级
            memberView.image = model?.user?.mbrankImage
            
            contentLabel.text = model?.text
        }
    }
    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        prepareUI()
    }
    
    // MARK: - 准备UI
    /// 准备UI
    func prepareUI() {
        // 添加子控件
        contentView.addSubview(iconView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(timeLabel)
        contentView.addSubview(verifiedView)
        contentView.addSubview(memberView)
        contentView.addSubview(contentLabel)
        
        /// 头像视图
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 35, height: 35), offset: CGPoint(x: CZStatusCellMargin, y: CZStatusCellMargin))
        
        /// 名称
        nameLabel.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 时间
        timeLabel.ff_AlignHorizontal(type: ff_AlignType.BottomRight, referView: iconView, size: nil, offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 会员等级
        memberView.ff_AlignHorizontal(type: ff_AlignType.CenterRight, referView: nameLabel, size: CGSize(width: 14, height: 14), offset: CGPoint(x: CZStatusCellMargin, y: 0))
        
        /// 认证图标
        verifiedView.ff_AlignInner(type: ff_AlignType.BottomRight, referView: iconView, size: CGSize(width: 17, height: 17), offset: CGPoint(x: 8.5, y: 8.5))
        
        /// 内容
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: CZStatusCellMargin))
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: -CZStatusCellMargin))
        
        // 底部约束
        contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: CZStatusCellMargin))
    }
    
    // MARK: - 懒加载
    /// 用户头像
    lazy var iconView = UIImageView()
    
    /// 用户名称
    lazy var nameLabel: UILabel = UILabel(color: UIColor.darkGrayColor(), fontSize: 14)
    
    /// 时间label
    lazy var timeLabel: UILabel = UILabel(color: UIColor.orangeColor(), fontSize: 9)
    
    /// 认证图标
    lazy var verifiedView = UIImageView()
    
    /// 会员等级
    lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    
    /// 内容
    lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.darkGrayColor(), fontSize: 16)
        
        label.numberOfLines = 0
        
        return label
    }()
}
